package com.qzdsoft.erpcloud.controller.proj;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.common.domain.query.sys.QueryParams;
import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.proj.MaterialReview;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryValueService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.proj.MaterialReviewService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.proj.MaterialReviewInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialReviewQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;

/**
 * Author: zhanghao
 * task: 物料审核
 * Date : 2017-08-16
 */

@Controller
@RequestMapping("/eng/material/review")
public class MaterialReviewController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(MaterialReviewController.class);
    private static final String BASEPATH = "eng/materialreview";
    @Autowired
    private MaterialReviewService reviewService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private DataDictionaryValueService enumValueService;
    @Autowired
    private CustomerSupplierService customerSupplierService;
    
    
    /**
     * 物料审核 初始化页面
     * @param request
     * @param model
     * @param params
     * @return
     */
    @Permission(validate=false)
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request,QueryParams params){
        ModelAndView mv = new  ModelAndView();
     // 获取物料分类信息 列表 根据分组id 拿数据
        mv.addObject("materialTypeList", enumValueService.getByEnumId(7));
        mv.addObject("supplierList", this.customerSupplierService.findAllSupplier());
        mv.setViewName("eng/materialreview/index");
        return mv;
    }

    /**
     * 初始化网格数据
     * @param request
     * @param model
     * @param params
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/data")
    @ResponseBody
    public LayTableResponse<MaterialReviewInfo> list(int page, int limit)throws Exception{
        // layui table 网格数据源
        LayTableResponse<MaterialReviewInfo> tabledata = reviewService.findPage(page, limit);
        return tabledata;
    }
    /**
     * 条件过滤查询
     * @param queries
     * @return
     * @throws Exception
     */
    @RequestMapping("/search")
    @ResponseBody
    public LayTableResponse<MaterialReviewInfo> searchlist(MaterialReviewQueryInfo queries)throws Exception{
        // layui table 网格数据源
        LayTableResponse<MaterialReviewInfo> tabledata = reviewService.findPageByItems(queries);
        return tabledata;
    }
    
	/**
	 * 物料审核新增页面 
	 * @param companyid
	 * @param id
	 * @return
	 */
    @Permission(validate=false)
	@RequestMapping("/goforadd")
    public ModelAndView addview(int companyid, Integer id){
        ModelAndView mv = new ModelAndView();
        // 获取物料分类信息 列表 根据分组id 拿数据
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        mv.addObject("supplierList", this.customerSupplierService.findAllSupplier());
        mv.setViewName(BASEPATH + "/addinfo");
        return mv;
    }

	/**
	 * 创建物料审核信息
	 * @param bom
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/addReview")
	public Response<String> addReview(@ModelAttribute MaterialReview review, HttpServletRequest request)
			throws Exception {
	    LoginUser loginUser = getUser();;
		// 提交保存
		return reviewService.addMaterialReview(review, loginUser);
	}
	
	/**
	 * 物料审核修改页面
	 * @param model
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@Permission(validate=false)
	@RequestMapping("/goforedit")
	public ModelAndView editview( HttpServletRequest request ,int id) throws Exception {
	    LoginUser loginUser = getUser();;
		ModelAndView mv = new ModelAndView();
		//将父件id写回到页面
		mv.addObject("entity", this.reviewService.getReviewViById(loginUser,id));
        mv.addObject("supplierList", this.customerSupplierService.findAllSupplier());
        mv.setViewName(BASEPATH + "/editinfo");
		return mv;
	}
	/**
	 * 修改物料审核信息
	 * @param childbom
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/editReview")
	public Response<String> editReview(@ModelAttribute MaterialReview review, HttpServletRequest request)
			throws Exception {
	    LoginUser loginUser = getUser();;
		// 提交保存
		return reviewService.editReview(review, loginUser);
	}
	/**
	 * 删除物料审核信息
	 * @param id
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/deleteReview")
	public Response<String> delById(Integer id, HttpServletRequest request) throws Exception {
		// 检查session
	    LoginUser loginUser = getUser();;
		return reviewService.delById(id, loginUser);
	}
	
	/**
	 * 审核
	 * @param id
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/audt")
	public Response<String> audtById(Integer id, HttpServletRequest request) throws Exception {
		// 检查session
	    LoginUser loginUser = getUser();;
		return reviewService.audtById(id, loginUser);
	}
	
	/**
	 * 弃审
	 * @param id
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/unaudt")
	public Response<String> unAudtById(Integer id, HttpServletRequest request) throws Exception {
		// 检查session
	    LoginUser loginUser = getUser();;
		return reviewService.unAudtById(id, loginUser);
	}
	/**
    * 物料审核查看
    * @param id
    * @return
	 * @throws Exception 
    */
	 @Permission(validate=false)
    @RequestMapping("/view/{id}")
    public ModelAndView view(@PathVariable("id")Integer id) throws Exception {
        logger.debug("物料审核查看:id={}",id);
        LoginUser loginUser = getUser();;
        ModelAndView mv = new ModelAndView();
        mv.addObject("entity", this.reviewService.getReviewViById(loginUser,id));
        mv.setViewName(BASEPATH+"/view");
        return mv;
    }
}
